package com.changfen.handler;

import com.lmax.disruptor.EventHandler;
import com.changfen.event.Event;
import com.changfen.enums.EventTypeEnum;
import com.changfen.util.BitUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @author changfeng.chen@things-matrix.com
 * @date 2022/8/26 21:23
 **/
@Slf4j
public abstract class AbstractEventHandler implements EventHandler<Event> {

    /**
     * @return
     */
    protected abstract EventTypeEnum getEventType();

    @Override
    public void onEvent(Event event, long sequence, boolean endOfBatch) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("event = {}, sequence = {}, endOfBatch = {}", event, sequence, endOfBatch);
        }
        EventTypeEnum eventType = getEventType();

        if(BitUtils.isOneInPosition(event.getBit(),eventType.getPosition())){
            invoke(event);
        }
    }
    protected abstract void invoke(Event event);
}
